﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace Dominion.Core
{
    public class FixedLengthQueue<T> : INotifyCollectionChanged, IEnumerable<T> 
    {
        private List<T> InnerList { get; set; }

        public FixedLengthQueue(int length)
            : base()
        {
            Length = length;
            InnerList = new List<T>(length);
        }

        public int Length
        {
            get;
            private set;
        }

        public T Dequeue()
        {
            T item = InnerList.First();
            InnerList.Remove(item);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Remove, item,0));
            return item;
        }

        public void Enqueue(T item)
        {
            if (InnerList.Count >= Length)
            {
                this.Dequeue();
            }
            InnerList.Add(item);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
        }

        #region INotifyCollectionChanged Members

        public event NotifyCollectionChangedEventHandler CollectionChanged;
        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(this, e);
            }
        }
        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return InnerList.GetEnumerator();
        }
        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return InnerList.GetEnumerator();
        }

        #endregion
    }
}
